<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc,fixuphtml" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>Java Object Serialization Specification: C - Example of Serializable Fields</title>
  <style type="text/css">
      code{white-space: pre-wrap;}
      span.smallcaps{font-variant: small-caps;}
      span.underline{text-decoration: underline;}
      div.column{display: inline-block; vertical-align: top; width: 50%;}
  </style>
  <link rel="stylesheet" href="../../resources/jdk-default.css" />
  <!--[if lt IE 9]>
    <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
  <![endif]-->
</head>
<body>
<header id="title-block-header">
<h1 class="title">Java Object Serialization Specification: C - Example of Serializable Fields</h1>
</header>
<main><ul>
<li><a href="#c.1-example-alternate-implementation-of-java.io.file">Example Alternate Implementation of java.io.File</a></li>
</ul>
<hr />
<h2 id="c.1-example-alternate-implementation-of-java.io.file">C.1 Example Alternate Implementation of java.io.File</h2>
<p>This appendix provides a brief example of how an existing class could be specified and implemented to interoperate with the existing implementation but without requiring the same assumptions about the representation of the file name as a <em>String</em>.</p>
<p>The system class <code>java.io.File</code> represents a filename and has methods for parsing, manipulating files and directories by name. It has a single private field that contains the current file name. The semantics of the methods that parse paths depend on the current path separator which is held in a static field. This path separator is part of the serialized state of a file so that file name can be adjusted when read.</p>
<p>The serialized state of a <code>File</code> object is defined as the serializable fields and the sequence of data values for the file. In this case, there is one of each.</p>
<pre><code>Serializable Fields:
    String path;     // path name with embedded separators
Serializable Data:
    char            // path name separator for path name</code></pre>
<p>An alternate implementation might be defined as follows:</p>
<pre><code>class File implements java.io.Serializable {
    ...
    private String[] pathcomponents;
    // Define serializable fields with the ObjectStreamClass

    /**
     * @serialField path String
     *              Path components separated by separator.
     */

    private static final ObjectStreamField[] serialPersistentFields
        = { new ObjectStreamField(&quot;path&quot;, String.class) };
    ...
        /**
         * @serialData  Default fields followed by separator character.
         */

    private void writeObject(ObjectOutputStream s)
        throws IOException
    {
        ObjectOutputStream.PutField fields = s.putFields();
        StringBuffer str = new StringBuffer();
        for(int i = 0; i &lt; pathcomponents; i++) {
            str.append(separator);
            str.append(pathcomponents[i]);
        }
        fields.put(&quot;path&quot;, str.toString());
        s.writeFields();
        s.writeChar(separatorChar); // Add the separator character
    }
    ...

    private void readObject(ObjectInputStream s)
        throws IOException
    {
        ObjectInputStream.GetField fields = s.readFields();
        String path = (String)fields.get(&quot;path&quot;, null);
        ...
        char sep = s.readChar(); // read the previous separator char

        // parse path into components using the separator
        // and store into pathcomponents array.
    }
}</code></pre>
</main><footer class="legal-footer"><hr/><a href="../../legal/copyright.html">Copyright</a> &copy; 1993, 2021, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.<br>All rights reserved. Use is subject to <a href="https://www.oracle.com/java/javase/terms/license/java17speclicense.html">license terms</a> and the <a href="https://www.oracle.com/technetwork/java/redist-137594.html">documentation redistribution policy</a>. <!-- Version 17.0.2+8-LTS-86 --></footer>
</body>
</html>